# -*- coding: utf-8 -*-
"""
@Time : 2024/10/27 22:01
@Author : 仰望天空的蜗牛
@Email : middlegod@sina.com
@File : get_live_weibo_search.py
@Description: 获取实时微博搜索
"""
import logging
import time
import urllib.parse

import requests
from bs4 import BeautifulSoup

from file_relation import if_saved_in_file, saved_to_file

LOG_FORMAT = "%(levelname)s %(asctime)s - %(message)s"
logging.basicConfig(filename='weibo_speaker.log', level=logging.INFO, filemode='a', format=LOG_FORMAT)
logger = logging.getLogger()


def get_mblog_long_text(mblog_id):
    """
    通过微博id获取微博完整文本
    :param mblog_id:
    :return:
    """
    logger.info(f"传入的微博id：{mblog_id}")
    url = f"https://weibo.com/ajax/statuses/longtext?id={mblog_id}"
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36",
        "Cookie": "SUB=_2AkMRBb7odcPxrAJZmv8WyWvhaolH-jyi0NceAn7uJhMyOhh87mYkqSdutBF-XAHavRIYgFDYmUOPezr3SUzDg2yl"}
    resp = requests.get(url, headers=headers).json()
    # logger.info(f"get_mblog_long_text获取到的数据：{resp}")
    mblog_longtext = resp.get("data").get("longTextContent")
    logger.info(f"微博完整文本：{mblog_longtext}")
    # print(resp.get("data").get("longTextContent"))
    return mblog_longtext


def get_weibo_search(keyword):
    """
    根据查询关键字获取微博查询结果
    :param keyword: 查询关键字
    :return:
    """
    # keyword = config_data.get('weibo').get("search").get("kw")
    # 进行url编码操作
    # 等同于浏览器开发者工具-控制台中的函数encodeURIComponent()
    encode_keyword = urllib.parse.quote(keyword)

    base_url = "https://s.weibo.com/realtime"

    result_set = []
    headers = {"X-Requested-With": "XMLHttpRequest",
               "Cookie": "SUB=_2A25KJc4HDeRhGedG6VUS9S7PzTiIHXVpW0_PrDV8PUJbkNAGLU_HkW1NUXc-KzbCIfNHISLwdBGL2m4GbYdPyUZf;",
               "Referer": f"https://s.weibo.com/realtime?q={encode_keyword}&rd=realtime&tw=realtime&Refer=pic_realtime"}

    # 已播放过的保存在该文件下
    record_file = 'already_play.txt'

    # 只获取分页 1页的搜索结果
    for page in range(1, 2):
        target_url = f"{base_url}?q={encode_keyword}&rd=realtime&tw=realtime&page={page}"
        # print(f"请求的地址是 {target_url}")
        logger.info(f"请求的地址是 {target_url}")
        r = requests.get(target_url, headers=headers)
        soup = BeautifulSoup(r.text, "html.parser")
        # print(soup.prettify())
        results = soup.find_all('div', class_="content")
        for result in results:
            user_name = result.find('a', class_="name").get_text()
            # print(user_name)
            user_url = result.find('a', class_="name").get('href')
            # print(user_url)
            created_at = result.find('div', class_='from').get_text().strip().replace(' ', '').replace('\n', '')
            # print(created_at)
            mblog_id = result.find('div', class_='from').find_all('a')[0].get('href').split('?')[0].split('/')[-1]
            # title = result.find('p', class_='txt').get_text().strip().replace(' ', '').replace('\n', '')
            title = result.find('p', class_='txt').get_text().strip()
            # print(title)
            if title.endswith('展开c'):
                logger.info(f"开始获取{user_name}发布博文{mblog_id}的长文本")
                title = get_mblog_long_text(mblog_id)
                logger.info(f"成功获取{user_name}发布博文{mblog_id}的长文本")
            # 判断是否已经播放过
            if_saved = if_saved_in_file(title, record_file)
            if if_saved:
                continue
            temp = {
                'title': title,
                'mblog_id': mblog_id,
                'created_at': created_at,
                'user_name': user_name,
                'user_url': user_url
            }
            result_set.append(temp)
            # 保存值已播放记录文件中
            saved_to_file(
                f"{time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())}：\n{title} \nfrom 【{user_name}】({user_url})\n\n",
                record_file)

    # print(f"爬取的数据条数{len(result_set)}~")
    logger.info(f"爬取的数据条数{len(result_set)}~")
    return result_set


if __name__ == '__main__':
    r = get_weibo_search('python')
    print(r)
